<?php

class RoomController extends Controller
{
	/**
	 * @return array action filters
	 */
	
	public function filters()
	{
		return array(
				'accessControl', // perform access control for CRUD operations
				//'postOnly + delete', // we only allow deletion via POST request
		);
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(

					
				array('allow', // allow authenticated user to perform 'create' and 'update' actions
						'actions'=>array('addnew','edit','index','update','delete','admin','list','view'),
						'users'=>array('@'),
				),
				array('deny',  // allow all users to perform 'index' and 'view' actions
						'users'=>array('*'),
				),
				/*
				 array('allow', // allow admin user to perform 'admin' and 'delete' actions
				 		'actions'=>array('admin','delete'),
				 		'users'=>array('admin'),
				 ),
		array('deny',  // deny all users
				'users'=>array('*'),
		),
		*/
		);
	}
	public function actionIndex()
	{
		$model=new Room('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Room']))
			$model->attributes=$_GET['Room'];

		$this->render('index',array(
				'model'=>$model,
		));
	}
	public function actionaddnew()
	{
		$model=new Room;

		// Uncomment the following line if AJAX validation is needed
		$this->performAjaxValidation($model);

		if(isset($_POST['Room']))
		{
			$model->attributes=$_POST['Room'];
			if($model->save())
				Yii::app()->cache->set("RoomCache".md5($id),$model,1000);
				$this->redirect(array('view','id'=>$model->Id));
		}

		$this->render('addnew',array(
			'model'=>$model,
		));
	}
	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Room']))
		{
			var_dump($_POST["Room"]);
			
			$model->attributes=$_POST['Room'];

			if($model->save())
				Yii::app()->cache->set("RoomCache".md5($id),$model,1000);
				$this->redirect(array('view','id'=>$model->Id));
		}

		$this->render('edit',array(
			'model'=>$model,
		));
	}
	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('@'));
	}
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$model = Yii::app()->cache->get("RoomCache".md5($id));
		$userList = Yii::app()->cache->get("RoomUserListCache".md5($id));
        //Yii::app()->cache->delete("RoomCache".md5($id));
		if($model===false){
           
			$model = Room::model()->findByPk($id);
            if($model->Creator!=0)
                $model->Creator = $model->roomowner->UserName;
         	$userList = $this->getUserListByRoomId($id);
			Yii::app()->cache->set("RoomCache".md5($id),$model,1000);
			Yii::app()->cache->get("RoomUserListCache".md5($id),$userList,1000);
		}

		$this->render('view',array(
				'model'=>$model,
				'userList'=>$userList,
		));
	}
	private function getUserListByRoomId($roomId){
		return array();
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Room::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='room-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}